package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.guoning.office_system.model.MeetingRoom;
import com.guoning.office_system.model.MeetingRoomWithUser;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.MeetingRoomService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/meetingRoom")
public class MeetingRoomController extends BaseController {
    @Autowired
    MeetingRoomService meetingRoomService;

    /**
     * 条件查询（分页）
     * @param pageIndex
     * @param pageSize
     * @param roomCodeOrName
     * @param roomType
     * @param enableStatus
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult selectByCondition(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "roomCodeOrName", defaultValue = "") String roomCodeOrName,
                                        @RequestParam(value = "roomType", defaultValue = "") Integer roomType,
                                        @RequestParam(value = "enableStatus", defaultValue = "") Integer enableStatus){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("roomCodeOrName", roomCodeOrName);
            criteria.addExtField("roomType", roomType);
            criteria.addExtField("enableStatus", enableStatus);
            pageBean = meetingRoomService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 修改状态
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateEnableStatus", method = RequestMethod.POST)
    public JsonResult updateEnableStatus(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            List<MeetingRoom> list = JSONArray.parseArray(params, MeetingRoom.class);
            for(int i=0; i<list.size(); i++){
                list.get(i).setModifier(user.getUserName());
            }
            meetingRoomService.updateEnableStatus(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 删除
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult updateForDelete(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            List<MeetingRoom> list = JSONArray.parseArray(params, MeetingRoom.class);
            for(int i=0; i<list.size(); i++){
                list.get(i).setModifier(user.getUserName());
            }
            meetingRoomService.updateForDelete(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 新增会议室
     * @param request
     * @param meetingRoom
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, MeetingRoom meetingRoom){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("roomCodeOrName", meetingRoom.getRoomName());
            Long count1 = meetingRoomService.selectByConditionCount(criteria);
            if(count1 != 0){
                return new JsonResult(Constants.STATUS_FAIL, "会议室名称已重复");
            }
            criteria.addExtField("roomCodeOrName", meetingRoom.getRoomCode());
            Long count2 = meetingRoomService.selectByConditionCount(criteria);
            if(count2 != 0){
                return new JsonResult(Constants.STATUS_FAIL, "会议室编码已重复");
            }
            User user = getUserDate(request);
            meetingRoom.setCreator(user.getUserName());
            Long id = meetingRoomService.insert(meetingRoom);
            if(id == null){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "新增失败");
            }else{
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "新增成功", id);
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "新增异常");
        }
        return jsonResult;
    }

    /**
     * 修改会议室
     * @param request
     * @param meetingRoom
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(HttpServletRequest request, MeetingRoom meetingRoom){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            meetingRoom.setModifier(user.getUserName());
            meetingRoom.setModifiedTime(new Date());
            int i = meetingRoomService.updateByPrimaryKeySelective(meetingRoom);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "修改成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "修改异常");
        }
        return jsonResult;
    }


}
